package com.springboot.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.mapper.UserMapper;
import com.springboot.model.bo.UserBO;
import com.springboot.model.po.Credit;
//import com.springboot.model.po.Credit;
import com.springboot.model.po.UserPO;
import com.springboot.service.CreditService;
//import com.springboot.service.CreditService;
import com.springboot.service.UserService;

import util.JWTUtil;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private CreditService creditService;

	@Override
	public int getUserById(String userId) {
		return userMapper.getUserById(userId);
	}

	@Override
	public String saveUser(UserPO user) {
		Map map = new HashMap();
		map.put(user.getUserId(), user.toString());
		String token = JWTUtil.createToken(map);
		if (userMapper.saveUser(user.getUserId(),user.getPassword(),user.getUserName(),user.getUserSex()) == 0)
			return null;
		return "success, token: " + token;
	}

	@Override
	public UserBO getUserByIdByPass(UserPO user) {
		// TODO Auto-generated method stub
		UserPO upo = userMapper.getUserByIdByPass(user);
		if (upo == null)
			return null;
		UserBO ubo = new UserBO();
		ubo.setPassword(upo.getPassword());
		ubo.setDelTag(upo.getDelTag());
		ubo.setMaxPoints(upo.getMaxPoints());
		ubo.setMemLevel(upo.getMemLevel());
		ubo.setPoints(upo.getPoints());
		ubo.setUserId(upo.getUserId());
		ubo.setUserImg(ubo.getUserImg());
		ubo.setUserName(upo.getUserName());
		ubo.setUserSex(upo.getUserSex());
		return ubo;
	}

	@Override
	public UserBO findSerect(String userId) {
		UserPO upo = userMapper.findSerect(userId);
		if (upo == null)
			return null;
		UserBO ubo = new UserBO();
		ubo.setPassword(upo.getPassword());
		return ubo;
	}

	@Override
	public BigDecimal getPoints(String userId) {
		// 查询积分（使用credit的流水来更新）
		BigDecimal points = creditService.getTotalCreditByUserId(userId);
		userMapper.updatePoints(userId, points);
		return userMapper.getPoints(userId);
//		return BigDecimal.ZERO;
	}

	@Override
	public Integer getMemLevel(String userId) {
		// 查询等级
		return userMapper.getMemLevel(userId);
	}

	@Override
	@Transactional
	public int spendPoints(String userId, BigDecimal points) {
		// 目前拥有积分
		BigDecimal pointsNow = userMapper.getPoints(userId);
		UserBO userBO = new UserBO();
		userBO.setUserId(userId);
		userBO.setPoints(pointsNow);
		if (!userBO.legal(points)) // points的有效性判断（非负且小于等于当前拥有的points）
			return 0;
		// 计算用户表中的总积分
		pointsNow = pointsNow.subtract(points);
		// 记录流水+更新删除位+过期处理
		Credit credit = new Credit();
		credit.setChannelType(1);// 兑换消耗
		credit.setNum(points.negate());// 买优惠券了
		credit.setUserId(userId);
		creditService.insertCredit(credit);
		// 更新
		return userMapper.updatePoints(userId, pointsNow);

	}

	@Override
	public BigDecimal getMaxPoints(String userId) {
		return userMapper.getMaxPoints(userId);
	}

	@Override
	public String login(String userId, String password) {
		UserPO user = new UserPO();
		user.setPassword(password);
		user.setUserId(userId);
		UserBO userFind = getUserByIdByPass(user);
		if (userFind == null)
			return "用户不存在！";
		Map<String, Object> map = new HashMap();
		String params = userFind.toString();
		map.put("userId", userId);
		return JWTUtil.createToken(map);
	}

	@Override
	public String getUsername(String userId) {
		return userMapper.getUsername(userId);
	}

}
